//
//  MainViewController.swift
//  wei
//
//  Created by 江齐松 on 15/7/28.
//  Copyright © 2015年 江齐松. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
}

    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setupButton()
    }

    func clickButton(){
        print(__FUNCTION__)
    }
    
    private func setupButton(){
        print(composeButton)
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count)
        let rect = CGRect(x: 2 * w, y: 0, width: w, height: tabBar.bounds.height)
        //composeButton.frame = CGRectOffset(rect, 2 * w, 0)
        composeButton.frame = rect

    }
    
    private func addChildViewControllers() {

        addChildViewController(HomeViewController(), title: "首页", imageName:  "tabbar_home")
        addChildViewController(MessagesViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
            
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(PofileViewController(), title: "个人", imageName: "tabbar_profile")
        
        
    }
    
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        vc.title = title
        let nav = UINavigationController(rootViewController: vc)
        nav.tabBarItem.image = UIImage(named: imageName)
        tabBar.tintColor = UIColor.orangeColor()
        //tabBar.barTintColor = UIColor.orangeColor()
        addChildViewController(nav)

    }


    lazy private var composeButton: UIButton = {
        //let button = UIButton(type: UIButtonType.ContactAdd)
        let button = UIButton()
        self.tabBar.addSubview(button)
        
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        

        button.addTarget(self, action: "clickButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
